package control;

import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
import java.util.Vector;

import javax.swing.ButtonGroup;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;

import boundary.BoardPanel;
import boundary.InventoryPanel;

import entity.Card.Item;
import entity.*;

public class ItemController implements ActionListener {
	private Pecies protector;
	private static InventoryPanel inventory;
	public void createItemCards(boolean isChinese){
		ReadFile rf;
		if(isChinese)
			rf = new ReadFile("/CardInfo/item_chi.txt");
		else
			rf = new ReadFile("/CardInfo/item_eng.txt");
		rf.readItemFile();
		for(int i=0; i<rf.getEffect().size(); i++){
			new Item (rf.getEffect().get(i),
					/**content*/
						rf.getResult().get(i*3+1),
						/**title*/
						rf.getResult().get(i*3),
						/**img,true for temp=>isChinese*/
						rf.getResult().get(i*3+2),isChinese);
		}
	}
	public void storeInventory(InventoryPanel ip) {
		ItemController.inventory = ip;
	}
	
	public void doRepaint(){
		ItemController.inventory.doRepaint();
	}
	
	public int drawItemCard() {
		Random rand = new Random();
		int index = rand.nextInt(Item.getCardSize());
		System.out.print(index);
		return index;
	}
	public Item displayItemCard(Pecies p) {
		if(p.getItemNumber() == 2){
			JOptionPane.showMessageDialog(null, "Your bag is full" , "Item full", JOptionPane.WARNING_MESSAGE,null);
		}
		else{
			int index = this.drawItemCard();
			Item currentCard = Item.getItemCard(index);			
			Object[] message = {currentCard.getItemName(),currentCard.getContent()};
			JOptionPane.showMessageDialog(null, message, "Item card", JOptionPane.INFORMATION_MESSAGE,currentCard.getImageIcon());
			Item cardCopy = currentCard.clone();
			cardCopy.setOwner(p);
			p.setItem(cardCopy);
			return currentCard;
		}
		return null;
	}
	public void setItemClickable(BoardPanel bp, InventoryPanel ip) {
		PeciesController peciesControl = new PeciesController();
		ArrayList<Pecies> al = peciesControl.getAllPecies();
		
		for(int i=0; i<al.size(); i++) {
			Pecies p = al.get(i);
			 Vector<Item> items = p.getAllItemsOnHand();
			 for(int j=0; j<items.size(); j++) {
				 items.get(j).setMouseListenerIsActive(false);
				 ip.removeMouseListener(items.get(j));
			 }
			if(p == bp.getMoveTokenObj()) {
				for(int j=0; j<items.size(); j++) {
					items.get(j).setMouseListenerIsActive(true);
					ip.addMouseListener(items.get(j));
				}
			}
		}
	}
	public void drawInventoryPanel(Graphics2D g,Pecies p,int x,int y){
		for(int i=0; i<p.getItemNumber();i++){
			Item currentCard = p.getItemOnHand(i);
			g.drawImage(currentCard.getImage(),x,y,30,30,null);
			currentCard.setX(x);
			currentCard.setY(y);
			x+=30;
		}
	}
	
	public void UpdateRoundUse(Pecies p){
		if(p.isItemUsing()){
			Item item = p.getItemUsing();
			int roundUse = item.getRoundUse();
			if(roundUse != 0){
				roundUse--;
				item.setRoundUse(roundUse);
			}
			if(roundUse == 0)
				p.setItemUsing(null);
		}		
	}
	
	public int useItemEffect(int effect, Pecies p){
		Item item = p.getItemUsing();
		switch(item.getEffectID()){
			case 1:
			case 2:
				if(effect <0)
					effect =0;
				break;
			case 3:
				effect*=2;
				break;
		}
		return effect;
	}
	
	public Pecies selectPecies(Pecies owner){
		PeciesController peciesControl = new PeciesController();
		ArrayList<Pecies> al = peciesControl.getAllPecies();
		Object[] message = new Object[al.size()+2];
		JRadioButton[] rb = new JRadioButton[al.size()-1]; 
		ButtonGroup group = new ButtonGroup();
		
		message[0] ="Item name";
		message[1] ="Please select a person:";
		
		int count=0,msg=2;
		for(int i=0; i<al.size(); i++) {
			if(al.get(i) != owner){
				rb[count] = new JRadioButton(al.get(i).getName());
				group.add(rb[count]);
				message[msg++]=rb[count];
				rb[count].setActionCommand(al.get(i).getName());
				rb[count++].addActionListener(this);
			}		
		}
		JOptionPane pane = new JOptionPane(message,  JOptionPane.QUESTION_MESSAGE);
		JDialog getTopicDialog =  pane.createDialog(null, "Select person");
		getTopicDialog.setVisible(true);
		getTopicDialog.setSize(300, 300);
		doRepaint();
		return protector;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		PeciesController peciesControl = new PeciesController();
		ArrayList<Pecies> al = peciesControl.getAllPecies();
		for(int i=0;i<al.size();i++){
			if(e.getActionCommand() == al.get(i).getName()){
				protector = al.get(i);
			}
		}	
	}
}
